﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SharingCenter.Web.Classes.Groups;

namespace SharingCenter.Web.Admin.Groups
{
    using SharingCenter.Web.Classes;

    public partial class List : AdminPageBase
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            ListView1.ItemCommand += new EventHandler<ListViewCommandEventArgs>(ListView1_ItemCommand);
            LinqDataSource1.Selecting += new EventHandler<LinqDataSourceSelectEventArgs>(LinqDataSource1_Selecting);
            btnSearch.Click += new EventHandler(btnSearch_Click);
        }

        void btnSearch_Click(object sender, EventArgs e)
        {
            ListView1.DataBind();
        }

        void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            if (IsPostBack)
            {
                e.Result = from t in scDataContext.Instance.Sc_Groups
                           where t.GroupName.Contains(txtSearchKeyword.Text.Trim())
                           select t;
            }
        }

        void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Enable":
                    DisableGroup(ref e);
                    break;
                default: break;
            }
        }

        private void DisableGroup(ref ListViewCommandEventArgs e)
        {
            Guid groupId = (Guid)ListView1.DataKeys[((ListViewDataItem)e.Item).DataItemIndex].Value;
            GroupOperations.EnableGroup(groupId, Convert.ToBoolean(e.CommandArgument));
            //ListView1.DataBind();
            this.RefreshPage();
        }

        protected void RegisterAsyncPostBack(object sender, EventArgs e)
        {
            ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl((Control)sender);
        }
    }
}